package com.andy.retrofitclient;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Actions;
import rx.internal.util.InternalObservableUtils;
import rx.schedulers.Schedulers;

/**
 * Created by Andy on 16/7/15.
 
 */
public class ResultManager {

    public enum ErrorTipType {
        none,
        toast,
        cusDialog
    }

    private Builder mBuilder;
    private Observable mObservable;

    private ResultManager(Builder builder) {
        mBuilder = builder;
        mObservable = builder.getObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(TransformerManager.progressTransformer(mBuilder));
    }


    public <T> void start(final Subscriber<T> subscriber) {
        mObservable.subscribe(subscriber);
    }

    public void start(Action1 onNext) {
        start(onNext, InternalObservableUtils.ERROR_NOT_IMPLEMENTED, Actions.empty());
    }

    public void start(Action1 onNext, Action0 onComplete) {
        start(onNext, InternalObservableUtils.ERROR_NOT_IMPLEMENTED, onComplete);
    }

    public void start(Action1 onNext, Action1 onError) {
        start(onNext, InternalObservableUtils.ERROR_NOT_IMPLEMENTED, Actions.empty());
    }

    public void start(Action1 onNext, Action1 onError, Action0 onComplete) {
        mObservable.subscribe(onNext, onError, onComplete);
    }


    /**
     * 1、Observable对象
     * 2、progressEnable 是否显示progressDialog
     * 3、progressDialog(可设置自定义的Dialog，没有自定义就是用默认的)
     * 4、errorShowType（none：显示，toast：显示toast，cusDilaog：自定义个的Dialog（如果没有设置自定义的Dialog，就默认为none））
     * 5、context 创建Dialog
     */

    public static class Builder {

        private Observable mObservable;
        private Context mContext;

        private boolean mShowProgress;
        private Dialog mProgressDialog;

        private ErrorTipType mErrTipType;
        private Dialog mCusErrDialog;


        public Builder setObservable(Observable observable) {
            mObservable = observable;
            return this;
        }

        public Builder setContext(Context context) {
            if (context == null) {
                throw new NullPointerException("context can not null");
            }
            mContext = context;
            return this;
        }

        public Builder setShowProgress(boolean enable) {
            mShowProgress = enable;
            return this;
        }

        public Builder setProgressDialog(Dialog dialog) {
            if (dialog == null)
                throw new NullPointerException("progress dialog can't null");

            mProgressDialog = dialog;
            mShowProgress = true;

            return this;
        }


        public Builder setErrorTipType(ErrorTipType type) {
            mErrTipType = type;
            return this;
        }

        public Builder setCusErrorDialog(Dialog dialog) {
            if (dialog == null)
                throw new NullPointerException("error dialog can't null");

            mCusErrDialog = dialog;
            mErrTipType = ErrorTipType.cusDialog;

            return this;
        }

        public ResultManager build() {
            return new ResultManager(this);
        }


        public Observable getObservable() {
            return mObservable;
        }

        public Context getContext() {
            return mContext;
        }

        public boolean isShowProgress() {
            return mShowProgress;
        }

        public Dialog getProgressDialog() {
            if (mShowProgress) {
                if (mProgressDialog == null) {
                    mProgressDialog = new ProgressDialog(mContext);
                    ((ProgressDialog) mProgressDialog).setMessage("正在请求网络");
                    ((ProgressDialog) mProgressDialog).setProgressStyle(ProgressDialog.STYLE_SPINNER);
                }
                return mProgressDialog;
            } else {
                return null;
            }
        }

        public ErrorTipType getErrTipType() {
            return mErrTipType;
        }

        public Dialog getCusErrDialog() {
            return mCusErrDialog;
        }
    }


}
